
// 系统动态调用脚本
var baseAjaxUrl,that = null,currentItem = null;
export function func(self, script) {
    that = self.VUE;
    baseAjaxUrl=self.baseURL;
    let newFunc = new Function('sys', script);
    newFunc(self);
};
export function request(url, param, method) {
    var obj = null;
    ajax({
        url: baseAjaxUrl + url,
        type: !method ? "POST" : method,
        dataType: 'json',
        data: param,
        success: function (result) {
            //请求成功后执行的代码
            obj = JSON.parse(result);
        },
        error: function (status) {
            //失败后执行的代码
        }
    });
    return obj;
}
//新增方法
export function Add(url, param, method) {
    var obj = null;
    if (IsJsonString(param)) {
        that.message("请求参数不是JSON类型", 3);
    };
    //判断param中的参数是否有值，没有的话直接赋默认值
    for (const key in param) {
        if (!param[key]) {
            console.log(!param[key], param[key]);
            switch (key) {
                case "funcId":
                    // param[key] = that.params && that.params.id ? null : that.menuId;
                    param[key] = 1640;
                    break;
                case "funcCode":
                    // param[key] = that.params && that.params.id ? that.params.menuCode : null;
                    param[key] = null;
                    break;
                case "type":
                    param[key] = 2;
                    break;
            }
        }
    };
    that.currentButtonDetail = currentItem;
    // + '&controlId=' + '按钮ID'
    let params = 'funcId=' + param.funcId + '&funcCode=' + param.funcCode + '&type=' + param.type;
    ajax({
        url: baseAjaxUrl + url,
        type: !method ? "POST" : method,
        dataType: 'json',
        data: params,
        ContentType: "application/x-www-form-urlencoded",
        success: function (result) {
            //请求成功后执行的代码
            if (JSON.parse(result).success) {
                if (JSON.parse(result).info.length === 1) {
                    that.funcId = that.params && that.params.id ? null : that.menuId;
                    that.funcCode =
                        that.params && that.params.id ? that.params.menuCode : null;
                    that.funcName = that.pageContent.text;
                    that.tdeptId = JSON.parse(result).info[0].id;
                    if (that.params && that.params.businessInfo) {
                        if (Object.keys(that.params.businessInfo).length > 0) {
                            that.businessInfoParam = that.params.businessInfo;
                        }
                    }
                    if (that.params && that.params.cellParams) {
                        that.formateInitParam();
                    } else {
                        that.cellViewShow = true;
                    }
                } else {
                    that.deptMap = JSON.parse(result).info;
                    that.deptMapShow = true;
                }
            }
            that.nodeId = 1;
            obj = JSON.parse(result).success;
        },
        error: function (status) {
            //失败后执行的代码
            obj = false;
        }
    });
    return obj;
};
//编辑按钮
export async function Edit(url, param, method) {
    if (IsJsonString(param)) {
        that.message("请求参数不是JSON类型", 3);
    };
    if (that.checkedData.length === 0) {
        that.message("请选择需要编辑的数据", 3);
        return;
    }
    if (that.checkedData.length > 1) {
        that.message("不能同时编辑多条数据", 3);
        return;
    }
    const detail = JSON.parse(JSON.stringify(that.checkedData[0]));
    that.wpId = await that.getWpId(detail);
    if (!that.wpId) {
        that.message("没有表单 ID 无法编辑", 3);
        return;
    }
    console.log(JSON.stringify(detail),that.pageContent.primaryName)
    if (!detail[that.pageContent.primaryName]) {
        that.message("没有主键无法编辑", 3);
        return;
    }
    var obj = null;
    //判断param中的参数是否有值，没有的话直接赋默认值
    for (const key in param) {
        if (!param[key]) {
            switch (key) {
                case "funcId":
                    param[key] = that.params && that.params.id ? null : that.menuId;
                    break;
                case "funcCode":
                    param[key] = that.params && that.params.id ? that.params.menuCode : null;
                    break;
                case "tableName":
                    param[key] = that.pageContent.tableName;
                    break;
                case "id":
                    param[key] = detail[that.pageContent.primaryName];
                    break;
                case "keyName":
                    param[key] = that.pageContent.primaryName;
                    break;
            }

        }
    };
    let params = 'funcId=' + param.funcId + '&funcCode=' + param.funcCode + '&tableName=' + param.tableName + '&id=' + param.id + '&keyName=' + param.keyName;
    ajax({
        url: baseAjaxUrl + url,
        type: !method ? "POST" : method,
        dataType: 'json',
        data: params,
        ContentType: "application/x-www-form-urlencoded",
        success: function (result) {
            //请求成功后执行的代码
            obj = JSON.parse(result);
            if (JSON.parse(result).success) {
                that.workId = detail[that.pageContent.primaryName];
                that.funcName = that.pageContent.text;
                that.tdeptId = that.$store.state.user.info.deptId;
                //新增  解决先浏览后编辑后的表单缓存问题
                that.nodeId=1;
                if (that.params && that.params.businessInfo) {
                    if (Object.keys(that.params.businessInfo).length > 0) {
                        that.businessInfoParam = that.params.businessInfo;
                    }
                }
                if (that.params && that.params.cellParams) {
                    that.formateInitParam();
                } else {
                    that.cellViewShow = true;
                }
                obj = JSON.parse(result).success;
            } else {
                that.message("没有编辑权限", 3);
                obj = false;
            }
            that.that.checkedData=[];
        },
        error: function (status) {
            //失败后执行的代码
            obj = false;
        }
    });
    return obj;
};
//删除按钮
export function Delete(url, param, method) {
    var obj = null;
    if (IsJsonString(param)) {
        that.message("请求参数不是JSON类型", 3);
    };
    let ids = [];

    that.checkedData.forEach((item) => {
        ids.push(item[that.pageContent.primaryName]);
    });
    //判断param中的参数是否有值，没有的话直接赋默认值
    for (const key in param) {
        if (!param[key]) {
            switch (key) {
                case "pageId":
                    param[key] = that.pageContent.id;
                    break;
                case "values":
                    ids.forEach(element => {
                        param[key].push(element)
                    });
                    break;
                case "menuId":
                    param[key] = that.params && that.params.id ? null : that.menuId;
                    break;
                case "menuCode":
                    param[key] = that.params && that.params.id ? that.params.menuCode : null;
                    break;
            }

        }
        if(key=="values"){
            param[key].forEach((element,index) => {
                if(element.length==0){
                    param[key].splice(index,1);
                }
            });
            ids.forEach(element => {
                param[key].push(element)
            });
        }
    };
    if (that.checkedData.length === 0) {
        that.message("请选择需要删除的数据", 3);
        return;
    }
    console.log("param:::"+JSON.stringify(param))
    that.messageBox("确认删除？").then((c) => {
        if (c.action) {
            ajax({
                url: baseAjaxUrl + url,
                type: !method ? "POST" : method,
                dataType: 'json',
                data: param,
                success: function (result) {
                    //请求成功后执行的代码
                    if (JSON.parse(result).success) {
                        that.search(that.pageIndex);
                        that.message(JSON.parse(result).msg, 1);
                    } else {
                        that.message("数据已经存在流程中，不允许删除", 3);
                    }
                    // obj = JSON.parse(result);
                    obj = JSON.parse(result).success;
                },
                error: function (status) {
                    //失败后执行的代码
                    obj = false;
                }
            });
        }
    });
    return obj;
};
//查看流程
export function ViewFlow(wfInstId) {
    //判断param中的参数是否有值，没有的话直接赋默认值
    if (that.checkedData.length > 1) {
        that.message("不能同时查看多个流程", 3);
        return;
    }
    if (that.checkedData.length === 0) {
        that.message("请选择需要查看的流程", 3);
        return;
    }
    if (that.checkedData[0].WF_STATE === 0) {
        that.message("流程未上报", 3);
        return;
    }
    if (wfInstId == "") {
        wfInstId = that.checkedData[0].WFINST_ID;
    }
    that.flowViewShow = true;
    that.wfInstId = wfInstId;
};
//导出
export async function Export(url, param, method) {
    //导出
    // const arr = await that.getFormattingCondition();
    let obj = null;
    if (IsJsonString(param)) {
        that.message("请求参数不是JSON类型", 3);
    };
    //判断param中的参数是否有值，没有的话直接赋默认值
    const arr = await that.getFormattingCondition();
    for (const key in param) {
        if (!param[key]) {
            switch (key) {
                case "pageId":
                    param[key] = that.pageContent.id;
                    break;
                case "menuId":
                    param[key] = that.params && that.params.id ? null : that.menuId;
                    break;
                case "menuCode":
                    param[key] = that.params && that.params.id ? that.params.menuCode : null;
                    break;
                case "params":
                    param[key] = arr;
                    break;
            }
        }
    };
    ajax({
        url: baseAjaxUrl + url,
        type: !method ? "POST" : method,
        dataType: 'json',
        data: param,
        success: function (result) {
            //请求成功后执行的代码
            // obj = JSON.parse(result);
            if (JSON.parse(result).success) {
                window.open(process.env.VUE_APP_BASE_API + JSON.parse(result).info);
                that.message(JSON.parse(result).msg, 1);
                obj = true;
            } else {
                that.message(JSON.parse(result).msg, 3);
                obj = false;
            }
        },
        error: function (status) {
            //失败后执行的代码
            obj = false;
        }
    });
    return obj;
};
//导入
export function Import(url, param, method) {
    let obj = null;
    if (IsJsonString(param)) {
        that.message("请求参数不是JSON类型", 3);
    };
    //判断param中的参数是否有值，没有的话直接赋默认值
    for (const key in param) {
        console.log(!param[key], param[key])
        if (!param[key]) {
            switch (key) {
                case "funcId":
                    param[key] = that.params && that.params.id ? null : that.menuId;
                    break;
                case "funcCode":
                    param[key] = that.params && that.params.id ? that.params.menuCode : null;
                    break;
                case "type":
                    param[key] = 2;
                    break;
            }
        }
    };
    ajax({
        url: baseAjaxUrl + url,
        type: !method ? "POST" : method,
        dataType: 'json',
        data: param,
        success: function (result) {
            //请求成功后执行的代码
            if (JSON.parse(result).info.length === 1) {
                that.tdeptId = JSON.parse(result).info[0].id;
                that.form = {
                    handleWay: 1,
                    file: "",
                };
                that.file = null;
                that.errorMessageShow = false;
                that.importFileDialog = true;
            } else {
                that.deptMap = JSON.parse(result).info;
                that.deptMapShow = true;
            }
            obj = true;
        },
        error: function (status) {
            //失败后执行的代码
            obj = false;
        }
    });
    return obj;
};
export function setTabColumnWide(columnName, columnWidth) {
    /**
     * 根据列名动态修改列的宽度
     */
    that.col.forEach(element => {
        for (const key in element) {
            if (element[key] == columnName) {
                element["width"] = columnWidth.toString();
            }
        }
    });
};
export function getColFielNameByKey(columnName) {
    /*
     *@functionName:     js
     *@params1: 字段名（键值）
     *@description:通过键值获取该元素
     *@author: xuzhaoqian
     *@date: 2021-05-31 19:15:46
     *@version: V1.0.5
    */
   let rowData=null;
    that.col.forEach((element, index) => {
        for (const key in element) {
            if (element[key] == columnName) {
                rowData=element;
                return;
            }
        }
    });
    return rowData;
};
export function getButtonByKey(buttonName) {
    /*
     *@functionName:     getButtonByKey
     *@params1: buttonName   获取单个按钮的对象
     *@description:
     *@author: xuzhaoqian
     *@date: 2021-05-31 19:40:38
     *@version: V1.0.5
    */
    that.buttonComponents.forEach((element, index) => {
        for (const key in element) {
            if (element[key] == buttonName) {
                return element;
            }
        }
    });
};

export function setAut(code, autNames) {
    /*
     *@functionName:     setAut
     *@params1: pageId 界面ID ，默认从界面获取
     *@params2: code  按钮code
     *@params3: 权限中文名，多个权限以逗号分割
     *@description:界面配置中，初始化脚本自定义权限函数
     *@author: xuzhaoqian
     *@date: 2021-05-31 14:07:39
     *@version: V1.0.5
    */
    let obj = null;
    let param = 'pageId=' + that.routeId + '&controlCode=' + code + '&aut=' + autNames;
    ajax({
        url: baseAjaxUrl + "/role/bindAut",
        type: "POST",
        dataType: 'json',
        data: param,
        ContentType: "application/x-www-form-urlencoded",
        success: function (result) {
            //请求成功后执行的代码
            obj = true;
        },
        error: function (status) {
            //失败后执行的代码
            obj = false;
        }
    });
    return obj;
};
export async function haveAut(item) {
    /*
     *@functionName:     js
     *@params1: funcId 
     *@params2: funcCode
     *@params3: tableName
     *@params4: id
     *@params4: keyName
     *@params4: controlId
     *@params4: type
     *@description:  role/editAut2
     *@author: xuzhaoqian
     *@date: 2021-05-31 17:00:20
     *@version: V1.0.5
    */
    let obj = null,detail = null,id=null;
    if (!that.checkedData.length == 0) {
        detail = JSON.parse(JSON.stringify(that.checkedData[0]));
        id = detail[that.pageContent.primaryName];
        // that.wpId = await that.getWpId(detail);
    }
    let itemObj = await getItem(item);
    //新增 编辑 删除
    let funcId = that.params && that.params.id ? null : that.menuId;
    let funcCode = that.params && that.params.id ? that.params.menuCode : null;
    let tableName = that.pageContent.tableName;
    let keyName = that.pageContent.primaryName;
    let controlId = itemObj.id;
    let type = itemObj.type;
    let params = 'funcId=' + funcId + '&funcCode=' + funcCode + '&tableName=' + tableName + '&id=' + id + '&keyName=' + keyName + '&controlId=' + controlId + '&type=' + type;
    if (itemObj.type == null) {
        if (!that.wpId) {
            that.message("没有表单 ID 无法编辑", 3);
            return;
        }
        if (!detail[that.pageContent.primaryName]) {
            that.message("没有主键无法编辑", 3);
            return;
        }
    }
    ajax({
        url: baseAjaxUrl + "/role/editAut2",
        type: "POST",
        dataType: 'json',
        data: params,
        ContentType: "application/x-www-form-urlencoded",
        success: function (result) {
            //请求成功后执行的代码
            obj = true;
        },
        error: function (status) {
            //失败后执行的代码
            that.message("没有权限", 3);
            obj = false;
        }
    });
    return obj;
};
export function returnItem(item) {
    currentItem = item;
};
// 返回当前按钮的数据
function getItem(item) {
    /*
     *@functionName:     
     *@params1: item
     *@description: 获取当前点击按钮的ID
     *@author: xuzhaoqian
     *@date: 2021-05-31 16:46:18
     *@version: V1.0.5
    */
    let itemObj = {};
    switch (item.attr) {
        case "businessButtonAdd":
            //新增
            itemObj.type = 0;
            break;
        case "businessButtonDelete":
            //删除
            itemObj.type = 0;
            break;
        case "businessButtonEdit":
            //编辑
            itemObj.type = 0;
            break;
        case "businessButtonViewFlow":
            //查看流程
            itemObj.type = -1;
            break;
        case "businessButtonExport":
            //导出
            itemObj.type = -1;
            break;
        case "businessButtonImport":
            //导入
            itemObj.type = -1;
            break;
        case "businessButtonCustom":
            //自定义
            itemObj.type = -1;
            break;
    }
    itemObj.id = item.id;
    return itemObj;
};
export function setCurrentRow(currentRow){
/*
 *@functionName: js
 *@params1: 行 row
 *@params2: 参数值
 *@description:设置当前选择的行，获取当前选择的行
 *@author: xuzhaoqian
 *@date: 2021-06-01 15:36:20
 *@version: V1.0.5
*/
return that.tableData[currentRow];
};
export function setCurrentCol(currentCol){
    /*
     *@functionName:   setCurrentCol
     *@params1: currentCol 当前列
     *@description:设置当前选择的列
     *@author: xuzhaoqian
     *@date: 2021-06-01 15:49:44
     *@version: V1.0.5
    */
     return that.col[currentCol];
}
export function setCurrentValue(oldV,newV){
    /*
     *@functionName:     js
     *@params1: 参数1
     *@params2: 参数2
     *@params3: 参数3
     *@params4: 参数4
     *@description: 设置当前选择的单元格值
     *@author: xuzhaoqian
     *@date: 2021-06-01 16:53:05
     *@version: V1.0.5
    */
   that.rowData.forEach(item=>{
    //    item[that.columnData.property]
       
   })
}
export function setCurrentText(textValue){
    /*
     *@functionName:     setCurrentText
     *@params1: textValue 文本值
     *@description: 设置当前选择的单元格文本   that.rowData,  that.columnData;
     *@author: xuzhaoqian
     *@date: 2021-06-01 17:14:16
     *@version: V1.0.5
    */
    that.rowData[that.columnData.property]=textValue;
};
export function getCellValue(row,col){
    /*
     *@functionName:     js
     *@params1: row  行
     *@params2: col 列
     *@params3: 参数3
     *@params4: 参数4
     *@description: 返回单元格值   that.rowData,  that.columnData;
     *@author: xuzhaoqian
     *@date: 2021-06-01 16:59:56
     *@version: V1.0.5
    */
    let colName= that.col[col].prop;
   return colName;
}
export function getCellText(row, col){
    /*
     *@functionName:     getCellText
     *@params1: row  行
     *@params2: col  列
     *@description: 返回单元格文本
     *@author: xuzhaoqian
     *@date: 2021-06-01 18:37:23
     *@version: V1.0.5
    */
    let colName=that.col[col].prop;
    return that.tableData[row][colName];
};
export function setCellValue(row,col,value){
    /*
     *@functionName:     setCellValue
     *@params1: 参数1
     *@params2: 参数2
     *@params3: 参数3
     *@params4: 参数4
     *@description: 设置单元格值  （属性名） that.rowData,  that.columnData;
     *@author: xuzhaoqian
     *@date: 2021-06-01 18:43:27
     *@version: V1.0.5
    */
     that.col[col].prop=value;
}
export function setCellText(row,col,text){
    /*
     *@functionName:     
     *@params1: row  行
     *@params2: col  列
     *@params3: text  文本值
     *@description:设置单元格文本
     *@author: xuzhaoqian
     *@date: 2021-06-02 14:16:04
     *@version: V1.0.5
    */
    let colName=that.col[col].prop;
    that.tableData[row][colName]=text;
};
export function getFieldValue(recordIndex,fieldName){
    /*
     *@functionName:     js
     *@params1: recordIndex  第几条记录
     *@params2: fieldName   字段文本值
     *@description: 返回字段值
     *@author: xuzhaoqian
     *@date: 2021-06-01 18:57:47
     *@version: V1.0.5
    */
   let text="";
   let recordData= that.tableData[recordIndex]
   for (const key in recordData) {
       if (recordData[key]==fieldName) {
            text=key;
           return;
       }
   }
   return text;
}
export function getFieldText(recordIndex,fieldName){
    /*
     *@functionName:     getFieldText
     *@params1: recordIndex   第几条记录
     *@params2: fieldName  字段属性名
     *@description: 返回字段文本
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:15:15
     *@version: V1.0.5
    */
   let text="";
     let recordData= that.tableData[recordIndex]
     for (const key in recordData) {
         if (recordData[key]==fieldName) {
            text=recordData[key];
             return
         }
     }
     return text;
}
export function setFieldValue(recordIndex,fieldName,value){
    /*
     *@functionName:     js
     *@params1: recordIndex  第几条数据
     *@params2: fieldName    字段属性名
     *@params3: value   字段值
     *@description: 设置字段值  两种方法：一是使用JS字符串的replace方法进行替换，二是使用delete删除
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:17:49
     *@version: V1.0.5
    */
     let recordData= that.tableData[recordIndex]
     for (const key in recordData) {
         if (recordData[key]==fieldName) {
             let data= JSON.stringify(recordIndex).replace(key,value);
             recordData=JSON.parse(data);
            //  recordData[value]=recordData[key];
            //  delete recordData[key];
         }
     }
}
export function setFieldText(recordIndex,fieldName,text){
    /*
     *@functionName:     setFieldText
     *@params1:  recordIndex  第几条数据
     *@params2: fieldName    字段名
     *@params3: text   字段值
     *@description: 设置字段文本
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:28:01
     *@version: V1.0.5
    */
     let recordData= that.tableData[recordIndex]
     for (const key in recordData) {
         if (key==fieldName) {
            recordData[key]=text;
         }
     }
}
export function V(rowIndex,filedName){
    /*
     *@functionName:     js
     *@params1: 参数1
     *@params2: 参数2
     *@params3: 参数3
     *@params4: 参数4
     *@description: 字段方式快速访问值
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:48:00
     *@version: V1.0.5
    */
     let text="";
     let recordData= that.tableData[rowIndex]
     for (const key in recordData) {
         if (key==filedName) {
             text=recordData[key];
            return 
         }
     }
     return text;
}
export function G(rowIndex,colIndex){
    /*
     *@functionName:     js
     *@params1: 参数1
     *@params2: 参数2
     *@params3: 参数3
     *@params4: 参数4
     *@description: 坐标方式快速访问值
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:48:13
     *@version: V1.0.5
    */
     let rowData= that.tableData[rowIndex];
     let colData=that.col[colIndex];
     let text="";

     for (const key in rowData) {
         if (key==colData.prop) {
            text=rowData[key];
            return 
         }
     }
     return text;
}
export function getWorkTable(){
    /*
     *@functionName:     js
     *@params1: 参数1
     *@params2: 参数2
     *@params3: 参数3
     *@params4: 参数4
     *@description:  返回定义的列表对象
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:55:33
     *@version: V1.0.5
    */
   return that.tableData;
}
export function getWorkField(){
    /*
     *@functionName:     js
     *@params1: 参数1
     *@params2: 参数2
     *@params3: 参数3
     *@params4: 参数4
     *@description: 返回定义的字段对象
     *@author: xuzhaoqian
     *@date: 2021-06-01 19:56:07
     *@version: V1.0.5
    */
   return that.col;
}
export function openComponent(param){
    /*
     *@functionName:     openCompoent
     *@params1: param 组件内容
     *@description: 弹框函数
     *@author: xuzhaoqian
     *@date: 2021-06-07 09:23:28
     *@version: V1.0.5
    */
     let info=param;
     if(info.type==1){
        console.log("info",info)
        let dblParamIn=JSON.parse(info.dblParamIn);
        that.templateWindowShow=true;
        that.templateWindowParams.id=dblParamIn.id;
        that.templateWindowParams.menuCode=dblParamIn.menuCode;
        that.templateWindowParams.multipleChoice=dblParamIn.multipleChoice;
        that.templateWindowParams.dialogTitle=info.dblcomText;
        that.templateWindowParams.params=dblParamIn.params;
        that.dblParamOut=JSON.parse(info.dblParamOut);
    };
    if(info.type==2){
        //表单
        that.formBtnType="add";
        that.cellDbClick(JSON.parse(info.dblParamIn));
    }
    if(info.type==3){
        //自定义链接
        window.open(param);
    }
    if(info.type==4){
        //自定义页面
        console.log(JSON.stringify(info))
        let dblParamIn=JSON.parse(info.dblParamIn);
        // that.templateWindowShow=true;
        that.customComponent=true;
        that.componentType=4;
        that.userParams=dblParamIn;
        // that.userParams="123"
        that.relatedPageRoute=info.dblcomText;
    }
}
export function getCheckedRow(){
    /*
     *@functionName:     getCheckedRow
     *@params1: 参数1
     *@description: 获取选中的单行数据 返回单个值（该行的索引值）
     *@author: xuzhaoqian
     *@date: 2021-06-11 10:41:38
     *@version: V1.0.5
    */
   if(that.checkedData.length>0){
    return that.checkedData[0].RN;
   }else{
       that.message('请选择数据',3)
   }
   
};
export function getCheckedRows(){
    /*
     *@functionName:     getCheckedRows
     *@params1: 参数1
     *@description:  获取选中的多行数据 返回多个值（选中行的索引值）
     *@author: xuzhaoqian
     *@date: 2021-06-11 10:42:14
     *@version: V1.0.5
    */
     if(that.checkedData.length>0){
         let arr=[];
        that.checkedData.forEach(element => {
             arr.push(element.RN)
         });
        return arr;
       }else{
           that.message('请选择数据',3)
       }
}
export function getCheckedRowsData(){
    /*
     *@functionName:     getCheckedRows
     *@params1: 参数1
     *@description:  获取选中的数据
     *@author: xuzhaoqian
     *@date: 2021-06-11 10:42:14
     *@version: V1.0.5
    */
   return that.checkedData;
}
export function getCheckedRowDataByKey(keyName){
    /*
     *@functionName:     getCheckedRowsDataByKey
     *@params1: keyName
     *@description: 根据键获取值
     *@author: xuzhaoqian
     *@date: 2021-06-29 10:38:56
     *@version: V1.0.5
    */
   if(that.checkedData.length>1){
       that.message('请选择一条数据',3);
       return 
   }
   return that.checkedData[0][keyName];
}
export function getCheckedDataByName(name){
    /*
     *@functionName:     getCheckedDataByName
     *@params1: name 中文字段
     *@description:根据中文名返回值
     *@author: xuzhaoqian
     *@date: 2021-06-11 14:18:35
     *@version: V1.0.5
    */
   let text="";
   that.col.forEach(element => {
       for (const key in element) {
           console.log(element[key])
           if(element[key]==name){
               text=that.checkedData[0][element['prop']];
               return ;
           }
       }
   });
   return text;
}
export function getCheckedDataByRowAndName(row,name){
    /*
     *@functionName:     js
     *@params1: row  行
     *@params2: name  名称
     *@description:
     *@author: xuzhaoqian
     *@date: 2021-06-11 14:19:40
     *@version: V1.0.5
    */
     let text="";
     that.col.forEach(element => {
        for (const key in element) {
            if(element[key]==name){
                that.checkedData.forEach(item => {
                    if(item.RN==row){
                        text=item[key];
                        return 
                    }
                });
            }
        }
    });
    return text;
}
export function getCurrentUserId(){
    /*
     *@functionName:     getCurrentUserId
     *@description:获取当前用户ID
     *@author: xuzhaoqian
     *@date: 2021-06-25 14:56:47
     *@version: V1.0.5
    */
   return that.$store.state.user.info.id;
}
export function reutrnPageInfo(){
    /*
     *@functionName:     js
     *@description:返回弹出页面选择的数据  页面跳页面 返回数据
     *@author: xuzhaoqian
     *@date: 2021-07-12 15:56:35
     *@version: V1.0.5
    */
   return that.paramOut;
}
export function setExternalPageData(url,data){
/*
 *@functionName: js
 *@params1: 参数1 url  其他项目的页面URL地址
 *@params2: 参数2 data  需要传输的数据 参数格式为JSON字符串
 *@description:调用外部页面，并把数据放入cookie中
 *@author: xuzhaoqian
 *@date: 2021-07-15 16:22:11
 *@version: V1.0.5
*/
if(!IsJsonString(data)){
    return that.$message("data必须为json字符串",3);
}
let Cookies=require("js-cookie");
Cookies.set("externalPageData", data, {
    expires: 30,
  });
  //数据已存储，页面在浏览器新增标签页打开
  that.externalPageSrc=url;
  that.isShowExternalPage=true;
  
  console.log("externalPageSrc::::",that.externalPageSrc)
//   window.open(url);
};
export function getExternalPageData(cookieName){
/*
 *@functionName: js
 *@params1: 参数1 根据cookie的名字获取cookie中的值
 *@description:使用JS调用外部页面，并获取返回参数，参数格式为JSON
 *@author: xuzhaoqian
 *@date: 2021-07-15 15:08:08
 *@version: V1.0.5
*/
let Cookies=require("js-cookie");
let cookVal= Cookies.get(cookieName);
return cookVal;
};

//创建ajax函数
export function $ajax(options){
    console.log(options)
    options = options || {};
    options.type = (options.type || 'GET').toUpperCase();
    options.dataType = options.dataType || 'json';
    let params = options.data;//formatParams(options.data);

    //创建-第一步
    var xhr;
    //非IE6
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        //ie6及其以下版本浏览器
        xhr = ActiveXObject('Microsoft.XMLHTTP');
    }

    //接收-第三步
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            var status = xhr.status;
            if (status >= 200 && status < 300) {
                console.log(xhr)
                options.success && options.success(xhr.responseText, xhr.responseXML);
            } else {
                options.error && options.error(status);
            }
        }
    }
    //连接和发送-第二步
    if (options.type == 'GET') {
        xhr.open('GET', options.url + '?' + params, false);
        xhr.send(null);
    } else if (options.type == 'POST') {
        xhr.open('POST', options.url, false);
        //设置表单提交时的内容类型
        xhr.setRequestHeader("token", getCookie());
        if (options.ContentType == "application/x-www-form-urlencoded") {
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        } else {
            xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        }
        if (typeof params == 'object') {
            xhr.send(JSON.stringify(params));
        } else {
            xhr.send(params);
        }

    }
};
function ajax(options) {
    console.log("options:::" + JSON.stringify(options));
    options = options || {};
    options.type = (options.type || 'GET').toUpperCase();
    options.dataType = options.dataType || 'json';
    let params = options.data;//formatParams(options.data);

    //创建-第一步
    var xhr;
    //非IE6
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        //ie6及其以下版本浏览器
        xhr = ActiveXObject('Microsoft.XMLHTTP');
    }

    //接收-第三步
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            var status = xhr.status;
            if (status >= 200 && status < 300) {
                console.log(xhr)
                options.success && options.success(xhr.responseText, xhr.responseXML);
            } else {
                options.error && options.error(status);
            }
        }
    }
    //连接和发送-第二步
    if (options.type == 'GET') {
        xhr.open('GET', options.url + '?' + params, false);
        xhr.send(null);
    } else if (options.type == 'POST') {
        xhr.open('POST', options.url, false);
        //设置表单提交时的内容类型
        xhr.setRequestHeader("token", getCookie());
        if (options.ContentType == "application/x-www-form-urlencoded") {
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        } else {
            xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        }
        if (typeof params == 'object') {
            xhr.send(JSON.stringify(params));
        } else {
            xhr.send(params);
        }

    }
}
function getCookie() {
    let strcookie = document.cookie;//获取cookie字符串
    let arrcookie = strcookie.split('; ');//分割cookie变成数组
    //遍历匹配
    let tokenVal = "";
    arrcookie.forEach(element => {
        var arr = element.split('=');
        if (arr[0] == "Admin-Token") {
            tokenVal = arr[1];
        }
    });
    return tokenVal;
}
function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}

